package com.zeny.edu.eduservice.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zeny.edu.common.ResponseResult;
import com.zeny.edu.eduservice.entity.EduTeacher;
import com.zeny.edu.eduservice.entity.query.QueryTeacher;
import com.zeny.edu.eduservice.service.EduTeacherService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author zeny
 * @since 2020-02-24
 */
@CrossOrigin
@RestController
@RequestMapping("/eduservice/teacher")
public class EduTeacherController {

    @Autowired
    private EduTeacherService eduTeacherService;

    @GetMapping("/list")
    public ResponseResult getAllTeacherList() {
        List<EduTeacher> list = eduTeacherService.list(null);
        return ResponseResult.ok().data("items", list);
    }

    @DeleteMapping("/{id}")
    public ResponseResult deleteById(@PathVariable String id) {
        boolean result = eduTeacherService.deleteById(id);
        return result ? ResponseResult.ok() : ResponseResult.fail();
    }

    @GetMapping("/pageList/{page}/{size}")
    public ResponseResult getPageTeacherList(@PathVariable("page") Integer page, @PathVariable("size") Integer size) {
        Page<EduTeacher> eduTeacherPage = new Page<>(page, size);
        IPage<EduTeacher> teacherIPage = eduTeacherService.page(eduTeacherPage, null);
        return ResponseResult.ok().data("items", teacherIPage.getRecords()).data("total", teacherIPage.getTotal());
    }

    /**
     * 条件分页查询
     *
     * @param page
     * @param size
     * @param queryTeacher
     * @return
     */
    @PostMapping("/moreConditionPageList/{page}/{size}")
    public ResponseResult moreConditionPageList(@PathVariable("page") Integer page,
                                                @PathVariable("size") Integer size,
                                                @RequestBody(required = false) QueryTeacher queryTeacher) {
        Page<EduTeacher> eduTeacherPage = new Page<>(page, size);
        IPage<EduTeacher> eduTeacherIPage = eduTeacherService.pageListCondition(eduTeacherPage, queryTeacher);
        return ResponseResult.ok().data("items", eduTeacherIPage.getRecords()).data("total", eduTeacherIPage.getTotal());
    }

    /**
     * 添加 |更新老师
     *
     * @param eduTeacher
     * @return
     */
    @PostMapping("/addOrUpdateTeacher")
    public ResponseResult addTeacher(@RequestBody EduTeacher eduTeacher) {
        if (StringUtils.isNotBlank(eduTeacher.getId())) {
            //更新
            boolean update = eduTeacherService.updateById(eduTeacher);
            return update ? ResponseResult.ok().message("修改老师成功") : ResponseResult.fail().message("修改老师失败");
        }
        boolean save = eduTeacherService.save(eduTeacher);
        return save ? ResponseResult.ok().message("添加老师成功") : ResponseResult.fail().message("添加老师失败");
    }

    /**
     * 根据id查找老师
     * @param id
     * @return
     */
    @GetMapping("/getTeacherById/{id}")
    public ResponseResult getTeacherInfo(@PathVariable String id) {
        EduTeacher teacher = eduTeacherService.getById(id);
        return teacher != null ? ResponseResult.ok().data("teacher", teacher) : ResponseResult.fail().message("无法找到该教师");
    }

    @PostMapping("/login")
    public ResponseResult login() {
        return ResponseResult.ok().data("token","admin");
    }

    @GetMapping("/info")
    public ResponseResult info() {
        return ResponseResult.ok().data("roles","[admin]").data("name","admin").data("avatar","https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
    }



}

